package com.zixuan.gulicollege.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zixuan.gulicollege.common_utils.PageUtils;
import com.zixuan.gulicollege.eduservice.mapper.EduTeacherMapper;
import com.zixuan.gulicollege.eduservice.service.EduTeacherService;
import com.zixuan.gulicollege.entity.eduservice.EduTeacher;
import com.zixuan.gulicollege.entity.eduservice.vo.TeacherQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 讲师 服务实现类
 *
 * @author zixuan
 * @since 2021-04-28
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {


    @Autowired
    private EduTeacherService teacherService;

    @Override
    public Map<String, Object> pageListTeacher(Integer current, Integer limit) {
        // 创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        return PageUtils.pageInfo(pageTeacher, null, teacherService);
    }

    @Override
    public Map<String, Object> pageTeacherCondition(Integer current, Integer limit, TeacherQuery teacherQuery) {
        // 创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        // 构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            // 模糊查询
            wrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(level)){
            // 等于
            wrapper.eq("level", level);
        }
        // 这里的值填入的是数据库的字段名称
        if (!StringUtils.isEmpty(begin)){
            // 大于等于
            wrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)){
            // 小于等于
            wrapper.le("gmt_modified", end);
        }

        // 按照降序排列
        wrapper.orderByDesc("gmt_create");

        return PageUtils.pageInfo(pageTeacher, wrapper, teacherService);
    }

    @Cacheable(key = "'getTeacherList'", value = "teacher")
    @Override
    public List<EduTeacher> getTeacherList() {
        // 查询前4条名师
        QueryWrapper<EduTeacher> teacherWrapper = new QueryWrapper<>();
        teacherWrapper.orderByDesc("level");
        teacherWrapper.last("limit 4");
        return teacherService.list(teacherWrapper);
    }
}
